package cn.com.nes.site.controller.crm.saleorder;

import cn.com.nes.common.web.CommonResult;
import cn.com.nes.site.controller.crm.saleorder.vo.BatchSaleOrderPaymentSuccessVO;
import cn.com.nes.site.controller.crm.saleorder.vo.SaleOrderPaymentSuccessVO;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.bo.crm.sale.BatchSaleOrderPaymentSuccessBO;
import cn.com.nes.site.entity.bo.crm.sale.UnDispatchedSaleOrderPageBO;
import cn.com.nes.site.entity.vo.crm.sale.*;
import cn.com.nes.site.service.crm.order.SaleOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = {"销售管理-销售单管理"})
@Controller
@RequestMapping("/v2/crm/order")
public class SaleOrderInfoController {

    @Resource
    SaleOrderInfoService saleOrderInfoService;

    @ApiOperation("保存订单")
    @RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
    public @ResponseBody PageData saveOrder(@RequestBody @Validated SaleOrderInfoVO vo) throws Exception {
        return saleOrderInfoService.saveOrder(vo);
    }

    @ApiOperation("查询订单分页")
    @RequestMapping(value = "/getOrderPage", method = RequestMethod.GET)
    public @ResponseBody CommonResult<?> getOrderPage(SaleOrderPageVO vo) throws Exception {
        return CommonResult.page(saleOrderInfoService.getOrderPage(vo));
    }

    @ApiOperation(value = "销售订单支付成功")
    @RequestMapping(value = "/saleOrderPaymentSuccess", method = RequestMethod.POST)
    public @ResponseBody CommonResult<?> saleOrderPaymentSuccess(
            @RequestBody @Validated SaleOrderPaymentSuccessVO saleOrderPaymentSuccessVO) throws Exception {
        saleOrderInfoService.orderPaymentSuccess(saleOrderPaymentSuccessVO.convertToBo());
        return CommonResult.ok();
    }

    @ApiOperation("查询订单详情")
    @RequestMapping(value = "/getOrderDetail", method = RequestMethod.GET)
    public @ResponseBody PageData getOrderDetail(GetSaleOrderDeatilsVO vo) throws Exception {
        return saleOrderInfoService.getOrderDetail(vo);
    }

    @ApiOperation("删除订单")
    @RequestMapping(value = "/delOrderByOrderId", method = RequestMethod.POST)
    public @ResponseBody PageData delOrderByOrderId(@RequestBody @Validated DeleteSaleOrderVO vo) throws Exception {
        return saleOrderInfoService.delOrderByOrderId(vo);

    }

    @ApiOperation("启用订单")
    @RequestMapping(value = "/enableSaleOrder", method = RequestMethod.POST)
    public @ResponseBody PageData enableSaleOrder(@RequestBody @Validated EnableSaleOrderVO vo) throws Exception {
        return saleOrderInfoService.enableSaleOrder(vo.convertToEnableSaleOrderBO());
    }

    @ApiOperation("切换销售单状态")
    @RequestMapping(value = "/changeSaleOrderStatus", method = RequestMethod.POST)
    public @ResponseBody PageData changeSaleOrderStatus(
            @RequestBody @Validated ChangeOrderStatusVO vo) throws Exception {
        return saleOrderInfoService.changeSaleOrderStatus(vo.convertToChangeSaleOrderStatusBo());
    }

    @ApiOperation("复制销售单")
    @RequestMapping(value = "/copySaleOrder", method = RequestMethod.POST)
    public @ResponseBody PageData copySaleOrder(@RequestBody @Validated CopySaleOrderVO vo) throws Exception {
        return saleOrderInfoService.copySaleOrder(vo.convertToCopySaleOrderInfoBo());
    }

    @ApiOperation("标记订单派单状态")
    @RequestMapping(value = "/markSaleOrderDispatched", method = RequestMethod.POST)
    public @ResponseBody PageData markSaleOrderDispatched(
            @RequestBody @Validated MarkSaleOrderDispatchedVO vo) throws Exception {
        return saleOrderInfoService.markSaleOrderDispatched(vo);
    }

    @ApiOperation(value = "查询订单分页 - 安装工单入口", notes = "需要创建安装工单的销售单分页查询")
    @RequestMapping(value = "/getUnDispatchedSaleOrderPage", method = RequestMethod.GET)
    public @ResponseBody PageData getUnDispatchedSaleOrderPage(
            @Validated UnDispatchedSaleOrderPageVO vo) throws Exception {
        return saleOrderInfoService.getUnDispatchedSaleOrderPage(vo.convertToBo(new UnDispatchedSaleOrderPageBO()));
    }

    @ApiOperation(value = "销售订单批量确认")
    @RequestMapping(value = "/batchSaleOrderPaymentSuccess", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> batchSaleOrderPaymentSuccess(@RequestBody @Validated BatchSaleOrderPaymentSuccessVO vo) throws Exception {
        return CommonResult.ok(saleOrderInfoService.batchSaleOrderPaymentSuccess(vo.convertToBo(BatchSaleOrderPaymentSuccessBO.class)));
    }

}
